package com.yupi.yupicturebackend.manager.websocket;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.yupi.yupicturebackend.manager.auth.SpaceUserAuthManager;
import com.yupi.yupicturebackend.manager.auth.model.SpaceUserPermissionConstant;
import com.yupi.yupicturebackend.model.entity.Picture;
import com.yupi.yupicturebackend.model.entity.Space;
import com.yupi.yupicturebackend.model.entity.User;
import com.yupi.yupicturebackend.model.enums.SpaceTypeEnum;
import com.yupi.yupicturebackend.service.PictureService;
import com.yupi.yupicturebackend.service.SpaceService;
import com.yupi.yupicturebackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.annotation.Resource;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class WsHandshakeInterceptor implements HandshakeInterceptor {
    @Resource
    private UserService userService;

    @Resource
    private PictureService pictureService;

    @Resource
    private SpaceService spaceService;

    @Resource
    private SpaceUserAuthManager spaceUserAuthManager;
    /**
     * 握手前
     * @param request
     * @param response
     * @param wsHandler
     * @param attributes
     * @return
     * @throws Exception
     */
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
      if(request instanceof ServerHttpRequest){
         HttpServletRequest httpservletRequest =((ServletServerHttpRequest) request).getServletRequest();
         //从请求中获取参数
          String pictureId = httpservletRequest.getParameter("pictureId");
          if (StrUtil.isBlank(pictureId)) {
              log.error("缺少图片参数，拒绝握手");
              return false;
          }
          // 获取当前登录用户
          User loginUser = userService.getLoginUser(httpservletRequest);
          if (ObjUtil.isEmpty(loginUser)) {
              log.error("用户未登录，拒绝握手");
              return false;
          }
          // 校验用户是否有编辑当前图片的权限
          Picture picture = pictureService.getById(pictureId);
          if (ObjUtil.isEmpty(picture)) {
              log.error("图片不存在，拒绝握手");
              return false;
          }
          Long spaceId = picture.getSpaceId();
          Space  space = null;
          if(spaceId!=null){
              space = spaceService.getById(spaceId);
              if(ObjUtil.isEmpty(space)){
                  log.error("图片所在空间不存在，拒绝握手");
                  return false;
              }
              if(!space.getSpaceType().equals(SpaceTypeEnum.TEAM.getValue())){
                  log.error("图片所在空间不是团队空间，拒绝握手");
                  return false;
              }
          }
          List<String> permissionList = spaceUserAuthManager.getPermissionList(space, loginUser);
          if(!permissionList.contains(SpaceUserPermissionConstant.PICTURE_EDIT)){
              log.error("用户没有编辑图片的权限，拒绝握手");
              return false;
          }
          //设置用户登录信息到WebSocket会话中
          attributes.put("user",  loginUser);
          attributes.put("userId",  loginUser.getId());
          attributes.put("picture",  Long.valueOf(pictureId));
      }
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {

    }
}
